package com.example.demo.utli;

import com.example.demo.bean.ErrorCode;
import com.example.demo.bean.ThisSystemException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PhoneNumberUtil {

    /**
     * @param phone 字符串类型的手机号
     * 传入手机号,判断后返回
     * */
    public static void isPhone(String phone) {
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        if (phone.length() != 11) {
            throw new ThisSystemException(ErrorCode.CELL_PHONE_ON_11.getText(),ErrorCode.CELL_PHONE_ON_11.getStatus());
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            if (!isMatch) {
                throw new ThisSystemException(ErrorCode.PLEASE_FILL_CORRECT_PHONE.getText(),ErrorCode.PLEASE_FILL_CORRECT_PHONE.getStatus());
            }
        }
    }

}
